using System;
using Guestbook.business;
using Guestbook.business.service;

namespace Guestbook.domain
{
    public class GuestBook : BusinessBase<GuestBook, int>
    {
        #region - Field & Properties -

        private string _title;
        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }

        private bool _active;
        public bool Active
        {
            get { return _active; }
            set { _active = value; }
        }

        private string _userName;
        public string UserName
        {
            get { return _userName; }
            set { _userName = value; }
        }

        private string _userPassword;
        public string UserPassword
        {
            get { return _userPassword; }
            set { _userPassword = value; }
        }

        private string _userWebsite;
        public string UserWebsite
        {
            get { return _userWebsite; }
            set { _userWebsite = value; }
        }

        private string _userEmail;
        public string UserEmail
        {
            get { return _userEmail; }
            set { _userEmail = value; }
        }

        private string _code;
        public string Code
        {
            get { return _code; }
            set { _code = value; }
        }

        private DateTime _dateCreation;
        public DateTime DateCreation
        {
            get { return _dateCreation; }
            set { _dateCreation = value; }
        }

        private int _totalVisitors;
        public int TotalVisitors
        {
            get { return _totalVisitors; }
            set { _totalVisitors = value; }
        }

        private DateTime _lastMessagePosted;
        public DateTime LastMessagePosted
        {
            get { return _lastMessagePosted; }
            set { _lastMessagePosted = value; }
        }

        private int _totalMessages;
        public int TotalMessages
        {
            get { return _totalMessages; }
            set { _totalMessages = value; }
        }

        private StateList<Message> _messages;
        public StateList<Message> Messages
        {
            get { return _messages; }
        }

        #endregion

        public GuestBook()
        {
            _messages = new StateList<Message>();
        }

        #region - Base overrides -

        ///// <summary>
        ///// Saves the object to the data store (inserts, updates or deletes).
        ///// </summary>
        ///// <returns></returns>
        //public override SaveAction Save()
        //{
        //  SaveAction action = base.Save();
        //  if (action == SaveAction.Insert || action == SaveAction.Update)
        //    _Content = null;

        //  return action;
        //}

        /// <summary>
        /// Validates the Post instance.
        /// </summary>
        protected override void ValidationRules()
        {
            //AddRule("Title", "Title must be set", String.IsNullOrEmpty(Title));
            //AddRule("Content", "Content must be set", String.IsNullOrEmpty(Content));
        }

        /// <summary>
        /// Returns a Post based on the specified id.
        /// </summary>
        protected override GuestBook DataSelect(int id)
        {
            return GuestBookService.GetGuestBookById(id);
        }

        /// <summary>
        /// Updates the Post.
        /// </summary>
        protected override void DataUpdate()
        {
            GuestBookService.UpdateGuestBook(this);
        }

        /// <summary>
        /// Inserts a new post to the current BlogProvider.
        /// </summary>
        protected override void DataInsert()
        {
            GuestBookService.AddGuestBook(this);
        }

        /// <summary>
        /// Deletes the Post from the current BlogProvider.
        /// </summary>
        protected override void DataDelete()
        {
            GuestBookService.DeleteGuestBookById(this.Id);
        }

        /// <summary>
        /// Gets if the Post have been changed.
        /// </summary>
        public override bool IsChanged
        {
            get
            {
                if (base.IsChanged)
                    return true;

                if (Messages.IsChanged)
                    return true;

                return false;
            }
        }

        /// <summary>
        /// Marks the object as being an clean,
        /// which means not dirty.
        /// </summary>
        public override void MarkOld()
        {
            base.MarkOld();
        }

        /// <summary>
        /// Loads an instance of the object based on the Id.
        /// </summary>
        /// <param name="id">The unique identifier of the object</param>
        public new static GuestBook Load(int id)
        {
            GuestBook instance = new GuestBook();
            instance = instance.DataSelect(id);
            instance.Id = id;
            if (instance != null)
            {
                instance.MarkOld();
                return instance;
            }
            return null;

        }

        public static GuestBook Load(int id, int pageSize, int currentPage)
        {
            return GuestBookService.GetGuestBook(id, pageSize, currentPage);
        }

        public static bool IsExist(int id)
        {
            return GuestBookService.IsExist(id);
        }

        #endregion

        // To String Stuffs
        public override string ToString()
        {
            return base.ToString();
        }
    }
}
